package com.hxkj.service.impl;

import com.github.pagehelper.PageInfo;
import com.hxkj.client.service.FunctionServiceClient;
import com.hxkj.client.service.UserServiceClient;
import com.hxkj.domain.enums.FuncitonExceptionEnum;
import com.hxkj.domain.enums.UserExceptionEnum;
import com.hxkj.domain.po.AuthFunction;
import com.hxkj.domain.po.FunctionMenu;
import com.hxkj.domain.po.FunctionTree;
import com.hxkj.entity.po.Function;
import com.hxkj.service.FunctionService;
import com.platform.common.domain.exception.CustomException;
import com.platform.common.domain.result.ParameterDataStruct;
import com.platform.common.domain.result.ResultDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import com.platform.common.domain.result.ResultPageStruct;
import com.platform.common.domain.to.PageTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 功能service
 * <p>
 * Create by chenwei on 2018/5/23 15:18
 */
@Service
public class FunctionServiceImpl implements FunctionService {

    @Autowired
    private FunctionServiceClient functionServiceClient;

    @Autowired
    private UserServiceClient userServiceClient;


    public Function get(Long functionId) {
        return null;
    }

    public PageInfo<Function> get(PageTo pageTo, Function function) {
        return null;
    }

    public List<Function> get(Function function) {
        return null;
    }

    public int delete(Long functionId) {
        return 0;
    }

    public int delete(String functionIdStr) {
        return 0;
    }

    public int deleteById(Long functionId) {
        return 0;
    }

    public int delete(Function function) {
        return 0;
    }

    public int save(Function function) {
        return 0;
    }

    public int update(Function function) {
        return 0;
    }

    public Set<String> findPermissionsByAccount(String account) throws Exception {
        ResultDataStruct<Set<String>> resultDataStruct;
        try {
            resultDataStruct = userServiceClient.findPermissionsByAccount(account);
            if (resultDataStruct.getCode() == 200) {
                Set<String> functions = resultDataStruct.getData();
                return functions;
            }else{
               throw new CustomException(UserExceptionEnum.getEnumByCode(resultDataStruct.getCode()));
            }
        } catch (Exception e) {
            throw new CustomException(UserExceptionEnum.GET_FUNCTION_BY_ACCOUNT_FAIL);
        }

    }

    public List<FunctionMenu> findFunctionMenuByAccount(String account, String appKey) {
        return null;
    }

    public List<FunctionMenu> findFunctionMenuByRoleId(Long roleId, String appKey) {
        return null;
    }

    public List<Function> getFunctionsByAppKey(String appKey) {
        return null;
    }

    public List<Function> getFunctionsByAccount(String appCode, String account) {
        return null;
    }

    public List<Function> getFunctionsByRole(String appCode, Long roleId) {
        return null;
    }

    public Function getOneFunction(String appCode, Long functionId) {
        return null;
    }

    public Map<String, Object> getRoleFunction(Long roleId) {
        return null;
    }

    @Override
    public List<FunctionTree> getFunctions(AuthFunction authFunction) throws Exception {
        ParameterDataStruct<AuthFunction> parameterDataStruct =new ParameterDataStruct();
        authFunction.setSiteId(authFunction.getSiteId()==null?0L:authFunction.getSiteId());
        parameterDataStruct.setData(authFunction);
        try {
            ResultPageStruct fun = functionServiceClient.getAuthFunctions(parameterDataStruct);
            if (fun.getCode() == 200) {
                List<FunctionTree> functionTrees = fun.getData();
                return functionTrees;
            }
            throw new CustomException(FuncitonExceptionEnum.getEnumByCode(fun.getCode()));
        }catch (Exception e){
            throw new CustomException(FuncitonExceptionEnum.FUNCTION_QUERY_ERROR);
        }
    }


    public List<FunctionTree> getFunctions() throws Exception {
        ParameterDataStruct<AuthFunction> parameterDataStruct = new ParameterDataStruct();
        try {
            ResultPageStruct fun = functionServiceClient.getAuthFunctions(parameterDataStruct);
            if (fun.getCode() == 200) {
                List<FunctionTree> functionTrees = fun.getData();
                return functionTrees;
            }
            throw new CustomException(FuncitonExceptionEnum.getEnumByCode(fun.getCode()));
        } catch (Exception e) {
            throw new CustomException(FuncitonExceptionEnum.FUNCTION_QUERY_ERROR);
        }
    }

    @Override
    public void saveFunction(AuthFunction authFunction) throws Exception {
        try{
            ResultMessageStruct resultMessageStruct= functionServiceClient.saveAuthFunction(authFunction);
            if (resultMessageStruct.getCode() == 200) {
            }else{
                throw new CustomException(FuncitonExceptionEnum.getEnumByCode(resultMessageStruct.getCode()));
            }
        }catch(Exception e){
            throw  new CustomException(FuncitonExceptionEnum.FUNCTION_SAVE_ERROR);
        }
    }

    @Override
    public void updateFunction(AuthFunction authFunction) throws Exception{
        try{
            ResultMessageStruct resultMessageStruct= functionServiceClient.updateAuthFunction(authFunction);
            if (resultMessageStruct.getCode() == 200) {
            }else{
                throw new CustomException(FuncitonExceptionEnum.getEnumByCode(resultMessageStruct.getCode()));
            }
        }catch(Exception e){
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public Object queryAuthFunction(AuthFunction authFunction) throws Exception {
        try {
            ResultDataStruct resultDataStruct = functionServiceClient.queryAuthFunction(authFunction);
            if(resultDataStruct.getCode()==200){
                Object af =  resultDataStruct.getData();
                return  af;
            }
            throw new CustomException(FuncitonExceptionEnum.getEnumByCode(resultDataStruct.getCode()));
        }catch (Exception e){
            throw new CustomException(FuncitonExceptionEnum.FUNCTION_QUERY_ERROR);
        }
    }

    @Override
    public void deleteFunction(AuthFunction authFunction)throws Exception {
        try{
            ResultMessageStruct resultMessageStruct= functionServiceClient.deleteAuthFunction(authFunction);
            if (resultMessageStruct.getCode() == 200) {
            }else{
                throw new CustomException(FuncitonExceptionEnum.getEnumByCode(resultMessageStruct.getCode()));
            }
        }catch (CustomException e){
            throw e;
        } catch(Exception e){
            throw  new CustomException(FuncitonExceptionEnum.FUNCTION_DELETE_ERROR);
        }
    }

    @Override
    public void modifyOrderIds(Map<String, Object> map) throws Exception {
        try{
            ResultMessageStruct resultMessageStruct= functionServiceClient.modifyOrderId(map);
            if (resultMessageStruct.getCode() == 200) {
            }else{
                throw new CustomException(FuncitonExceptionEnum.getEnumByCode(resultMessageStruct.getCode()));
            }
        }catch (CustomException e){
            throw e;
        } catch(Exception e){
            throw  new CustomException(FuncitonExceptionEnum.FUNCTION_UPDATE_ERROR);
        }
    }
}
